<?	/*
	// File:	cron.php
	// Purpose:	web-cp database-based task scheduler
	// Creation:	2004-11-20
	// Author:	Peter Drake
	*/

global $cfg;

// Indicate the backend is up
touch($cfg['basedir'].$cfg['statustag']);

// get current time
$minute = intval(date("i"));
$hour = intval(date("H"));
$day = intval(date("w")) + 1;
$week = intval(date("w")) + 1;
$month = intval(date("m"));

// connect to database
@mysql_close();
if (!@mysql_connect($cfg['dbhost'], $cfg['dbuser'], $cfg['dbpass'], true)) {
	sleep(1);
	if(!@mysql_connect($cfg['dbhost'], $cfg['dbuser'], $cfg['dbpass'])) {
		webcp_log(0,0,"cron","Could not connect to database server",0);
		return;
	}
}
		@mysql_select_db($cfg['dbname']);

		// select cron jobs to be run now
		$SQL = "SELECT id, commands, include, fork, user, domainid, timeout, phpcommands, hook FROM crons WHERE (month = '$month' OR month = '*') AND (week = '$week' OR week = '*') AND (day = '$day' OR day = '*') AND (hour = '$hour' OR hour = '*') AND (minute = '$minute' OR minute = '*') ORDER BY user = 'webcp' desc";
		$cronjobs = mysql_query($SQL);
		$result = -1;

		// Take action based on selected jobs
		for ($i = 0; $job = @mysql_fetch_array($cronjobs); $i++) {

			// get the real username if necessary
			$data['username'] = $job['user'];
			$data['id'] = $job['domainid'];
			call_hook('user:get:username', $data);

			// check whether the job needs to be forked or run inline
			switch($job['fork']) {
				// run job in fork (new process)
				case 1:
					// fork the process
					$pid = pcntl_fork();

					// check to ensure the process forked
					if ($pid == -1) {
						// log fork failures
						webcp_log(0,0,"webcp.php","Fork Error: could not fork webcp.php for ".$job['include'],0,$echo);
					} elseif (!$pid) {
						// Log Process ID
						$mypid = getmypid();
						file_put_contents($cfg['pid'].'-cronjob',$mypid);

						// Set timeout
						set_time_limit($job['timeout']);

						// if the cron job includes commands, run them
						if ($job['commands'] != "") {
							// exec the command
							exec($cfg['prog']['sudo']." -u ".$data['username']." ".$job['commands'], $cronresults, $result);
						}

						// if the cron job includes PHP commands, run them
						if ($job['phpcommands'] != "") {
							// run php commands
							eval($job['phpcommands']);
						}

						// if the cron job has PHP includes, run them
						if ($job['include'] != "") {
							// include cron script
							include($job['include']);
						}

						// if the cron job has a hook, run it
						if ($job['hook'] != "") {
							// include cron script
							call_hook($job['hook'], $data);
						}

						// Record results
						if (isset($cronresults)) {
							if (is_array($cronresults)) {
								$cronresults = implode("\n", $cronresults);
							}
							$SQL = "UPDATE crons SET lastrunresult = $result, lastrunoutput = '$cronresults', lastrun = NOW() WHERE id = '".$job['id']."'";
						} else {
							$SQL = "UPDATE crons SET lastrunresult = $result, lastrunoutput = 'no output', lastrun = NOW() WHERE id = '".$job['id']."'";
						}
						mysql_query($SQL);

						// exit the forked process
						exit;
					}
					break;

				// run job inline (in this process)
				case 0:

					// if the cron job includes commands, run then
					if ($job['commands'] != "") {
						// exec the command
						exec($cfg['prog']['sudo']." -u ".$data['username']." ".$job['commands'], $cronresults, $result);
					}

					// if the cron job includes PHP commands, run them
					if ($job['phpcommands'] != "") {
						// run php commands
						eval($job['phpcommands']);
					}

					// if the cron job has PHP includes, run them
					if ($job['include'] != "") {
						// include cron script
						include($job['include']);
					}

					// if the cron job has a hook, run it
					if ($job['hook'] != "") {
						// include cron script
						call_hook($job['hook'], $data);
					}

					// Record results
					if (isset($cronresults)) {
						if (is_array($cronresults)) {
							$cronresults = implode("\n", $cronresults);
						}
						$SQL = "UPDATE crons SET lastrunresult = $result, lastrunoutput = '$cronresults', lastrun = NOW() WHERE id = '".$job['id']."'";
					} else {
						$SQL = "UPDATE crons SET lastrunresult = $result, lastrunoutput = 'no output', lastrun = NOW() WHERE id = '".$job['id']."'";
					}
					mysql_query($SQL);

					break;
			}
		}
		mysql_close();

?>
